package com.gitee.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gitee.bo.CookbookBo;
import com.gitee.enums.CookbookTypeEnum;
import com.gitee.enums.ResponseEnum;
import com.gitee.exception.CookbookException;
import com.gitee.mapper.CookbookMapper;
import com.gitee.pojo.Cookbook;
import com.gitee.pojo.CookbookFood;
import com.gitee.service.ICookbookFoodService;
import com.gitee.service.ICookbookService;
import com.gitee.vo.CookbookVo;
import com.gitee.vo.PageVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class CookbookServiceImpl extends ServiceImpl<CookbookMapper, Cookbook> implements ICookbookService {

    @Autowired
    private CookbookMapper cookbookMapper;

    @Autowired
    private ICookbookFoodService cookbookFoodService;

    /**
     * 菜谱列表
     *
     * @param eatTime
     * @param cookbookType
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public PageVo<CookbookVo> pageCookbookVos(String eatTime, Integer cookbookType, Integer pageNum, Integer pageSize) {
        List<CookbookVo> cookbookVos = cookbookMapper.selectCookbookVos((pageNum - 1) * pageSize, pageSize, eatTime, cookbookType);
        LambdaQueryWrapper<Cookbook> queryWrapper = new QueryWrapper<Cookbook>().lambda()
                .eq(eatTime != null, Cookbook::getEatTime, eatTime)
                .eq(cookbookType != null, Cookbook::getCookbookType, cookbookType);
        Integer total = super.count(queryWrapper);
        return new PageVo<>(cookbookVos, total);
    }

    /**
     * 添加菜谱
     *
     * @param cookbookBo
     * @return
     */
    @Transactional
    @Override
    public Boolean addCookbook(CookbookBo cookbookBo) {
        LambdaQueryWrapper<Cookbook> lqw = new QueryWrapper<Cookbook>().lambda()
                .eq(Cookbook::getEatTime, cookbookBo.getEatTime())
                .eq(Cookbook::getCookbookType, CookbookTypeEnum.of(cookbookBo.getCookbookType()));
        if (super.count(lqw) > 0) {
            throw new CookbookException(ResponseEnum.COOKBOOK_EXIST);
        }
        Cookbook cookbook = new Cookbook();
        BeanUtils.copyProperties(cookbookBo, cookbook);
        cookbook.setCookbookType(CookbookTypeEnum.of(cookbookBo.getCookbookType()));
        super.save(cookbook);
        List<CookbookFood> cookbookFoods = cookbookBo.getFoodStockList().stream().map(foodStock -> {
            CookbookFood cookbookFood = new CookbookFood();
            cookbookFood.setCookbookId(cookbook.getId());
            cookbookFood.setFoodId(foodStock.getId());
            cookbookFood.setStock(foodStock.getStockNum());
            return cookbookFood;
        }).collect(Collectors.toList());
        return cookbookFoodService.saveBatch(cookbookFoods);
    }

    /**
     * 更新菜谱
     *
     * @param cookbookBo
     * @param cookbookId
     * @return
     */
    @Transactional
    @Override
    public Boolean updateCookbook(CookbookBo cookbookBo, Integer cookbookId) {
        LambdaQueryWrapper<Cookbook> lqw = new QueryWrapper<Cookbook>().lambda()
                .eq(Cookbook::getEatTime, cookbookBo.getEatTime())
                .eq(Cookbook::getCookbookType, CookbookTypeEnum.of(cookbookBo.getCookbookType()))
                .ne(Cookbook::getId, cookbookId);
        Cookbook existCookbook = super.getOne(lqw);
        if (existCookbook != null && !existCookbook.getId().equals(cookbookId)) {
            throw new CookbookException(ResponseEnum.COOKBOOK_EXIST);
        }
        Cookbook cookbook = new Cookbook();
        BeanUtils.copyProperties(cookbookBo, cookbook);
        cookbook.setCookbookType(CookbookTypeEnum.of(cookbookBo.getCookbookType()));
        cookbook.setId(cookbookId);
        super.updateById(cookbook);
        cookbookFoodService.removeByCookbookId(cookbookId);
        List<CookbookFood> cookbookFoods = cookbookBo.getFoodStockList().stream().map(foodStock -> {
            CookbookFood cookbookFood = new CookbookFood();
            cookbookFood.setCookbookId(cookbook.getId());
            cookbookFood.setFoodId(foodStock.getId());
            cookbookFood.setStock(foodStock.getStockNum());
            return cookbookFood;
        }).collect(Collectors.toList());
        return cookbookFoodService.saveBatch(cookbookFoods);
    }

    /**
     * 删除菜谱
     *
     * @param cookbookId
     * @return
     */
    @Transactional
    @Override
    public Boolean deleteCookbook(Integer cookbookId) {
        return super.removeById(cookbookId) && cookbookFoodService.removeByCookbookId(cookbookId);
    }

    /**
     * 批量删除菜谱
     *
     * @param cookbookIds
     * @return
     */
    @Transactional
    @Override
    public Boolean deleteCookbookBatch(Integer[] cookbookIds) {
        return super.removeByIds(Arrays.asList(cookbookIds)) && cookbookFoodService.removeByCookbookIds(cookbookIds);
    }
}
